package com.ruoyi.resume.mapper;

import com.ruoyi.resume.domain.model.ResumeTag;
import com.ruoyi.resume.domain.response.ResumeMatchResp;
import com.ruoyi.resume.provider.ResumeTagSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResumeTagMapper {

    @InsertProvider(type = ResumeTagSqlProvider.class, method = "insertResumeTag")
    int insertResumeTag(@Param("resumeTag") ResumeTag resumeTag);

    @SelectProvider(type = ResumeTagSqlProvider.class, method = "selectResumeTagsByResumeId")
    @Results({
            @Result(property = "resumeId", column = "resume_id"),
            @Result(property = "tagId", column = "tag_id"),
            @Result(property = "tagValue", column = "tag_value")
    })
    List<ResumeTag> selectResumeTagsByResumeId(@Param("resumeId") Integer resumeId);

    @DeleteProvider(type = ResumeTagSqlProvider.class, method = "deleteResumeTag")
    int deleteResumeTag(@Param("resumeId") Integer resumeId, @Param("tagId") Integer tagId);

    @Select("SELECT COUNT(*) FROM resume_tag WHERE resume_id = #{resumeId} AND tag_id = #{tagId}")
    int countResumeTag(@Param("resumeId") Integer resumeId, @Param("tagId") Integer tagId);

    /**
     * 注意：没有加@Results注解前,返回的列表每个元素均为null,可能的原因为字段没有正确映射到实体中
     * 在Mybatis的配置文件中设置驼峰匹配即可
     * */
    @Select("SELECT  resume_id, tag_id, tag_value FROM resume_tag")
    @Results({
            @Result(property = "resumeId", column = "resume_id"),
            @Result(property = "tagId", column = "tag_id"),
            @Result(property = "tagValue", column = "tag_value")
    })
    List<ResumeTag> getAllResumeTag();

    // 删除 resume_tag 表中与 tagId 相关的所有行
    @Delete("DELETE FROM resume_tag WHERE tag_id = #{tagId}")
    int deleteByTagId(@Param("tagId") Integer tagId);

    // 删除 resume_tag 表中与 resumeId 相关的所有行
    @Delete("DELETE FROM resume_tag WHERE resume_id = #{resumeId}")
    void deleteByResumeId(int resumeId);

    // 查询目标简历的标签值
    @Select("SELECT tag_value FROM resume_tag WHERE resume_id = #{resumeId}")
    List<String> findTagsByResumeId(@Param("resumeId") int resumeId);

    // 查询与目标简历相似的简历及公共标签数量
    @Select("<script>" +
            "SELECT rt.resume_id AS resumeId, COUNT(rt.tag_value) AS commonTagCount " +
            "FROM resume_tag rt " +
            "WHERE rt.tag_value IN " +
            "<foreach item='tag' collection='tags' open='(' separator=',' close=')'>#{tag}</foreach> " +
            "AND rt.resume_id != #{resumeId} " +
            "GROUP BY rt.resume_id " +
            "ORDER BY commonTagCount DESC " +
            "LIMIT 10" +
            "</script>")
    @Results({
            @Result(property = "resumeId", column = "resumeId"),
            @Result(property = "commonTagCount", column = "commonTagCount")
    })
    List<ResumeMatchResp> findSimilarResumesAndTagCount(@Param("tags") List<String> tags, @Param("resumeId") int resumeId);

    // 根据标签列表查询匹配的简历ID
    @Select("<script>" +
            "SELECT resume_id " +
            "FROM resume_tag " +
            "WHERE tag_value IN " +
            "<foreach item='tag' collection='tags' open='(' separator=',' close=')'>#{tag}</foreach> " +
            "GROUP BY resume_id " +
            "HAVING COUNT(DISTINCT tag_value) = #{size} " +
            "</script>")
    List<Integer> findResumeIdsByTags(@Param("tags") List<String> tags, @Param("size") int size);

}
